<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>组织架构</h2>
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!--头部组件-->
        <tree-tools :tree-data="company" :is-root="true" @openDialog="openDialog"/>
        <!-- 下面就是树形控件内容了 -->
        <el-tree :data="data" :props="{label:'name',children:'children'}">
          <!--再次复用头部组件-->
          <tree-tools
            slot-scope="{data}"
            :tree-data="data"
            @openDialog="openDialog"
            @updateList="initData"
            @editDialog="editDialog"
          />
        </el-tree>
      </el-card>
    </div>

    <!--新增和修改的弹框-->
    <add-dept ref="addDept" @updateList="initData"/>
  </div>
</template>

<script>
import TreeTools from '@/views/departments/components/tree-tools.vue'
import AddDept from '@/views/departments/components/add-dept.vue'
import { getDepartmentListApi } from '@/api/department'
import { tranListToTreeData } from '@/utils'

export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      data: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取数据
    async initData() {
      const { companyName, depts } = await getDepartmentListApi()
      this.company.name = companyName
      this.data = tranListToTreeData(depts, '')
    },
    // 打开弹框事件
    openDialog(val) {
      console.log('父部门id为' + val)
      this.$refs.addDept.dialogShow = true
      this.$refs.addDept.formData.pid = val
    },
    // 编辑部门事件
    editDialog(val) {
      console.log('编辑部门id为' + val)
      this.$refs.addDept.getFormDataInfo(val)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
